<?php
namespace app\admin\controller;

use app\common\controller\Back;

class Users extends Back
{
    private $pageSize = 20;
    
    protected function initialize()
    {
        parent::initialize();
    }
    
    public function index()
    {
        $args = [
            'where' => [],
        ];
        //搜索
        if($this->query['searchText']){
            array_push($args['where'],['user_mobile|user_name|user_email','like','%'.$this->query['searchText'].'%']);
        }
        //排序
        if(in_array($this->query['sortOrder'],['user_id','user_score'])){
            $args['order'] = [
                $this->query['sortOrder'] => 'desc',
            ];
        }else{
            $args['order'] = [
                'user_id' => 'desc',
            ];
        }
        //查询数据
        $list = model('common/User','loglic')->selectPage($args,$this->pageSize,$this->site['page'],false,$this->query);
        // 获取分页显示
        //$total = $list->total();
        $pages = $list->render();
        // 模板变量赋值
        $this->assign('list', $list);
        $this->assign('pages', $pages);
        $this->assign($this->site);
        // 渲染模板输出
        return $this->fetch();
    }
    
    public function create()
    {
        $form = dcFormCreate([
            'name'      => 'adminUserCreate',
            'action'    => dcUrl('admin/users/save'),
            'class'     => 'w-100',
            'reset'     => '重置',
            'items'     => $this->formItems($this->site['query']),
        ]);
        $this->assign('form', $form);
        $this->assign($this->site);
        return $this->fetch();
    }
    
    public function edit()
    {
        if(!$id = input('id/d',0)){
            $this->error($this->error,'admin/users/index');
        }
        //查询数据
        $result = model('common/User','loglic')->getId($id);
        if(!$result){
            $this->error($this->error,'admin/users/index');
        }
        //生成表单
        $form = dcFormCreate([
            'name'      => 'adminTestCreate',
            'action'    => dcUrl('admin/users/update'),
            'class'     => 'w-100',
            'reset'     => '重置',
            'items'     => $this->formItems($result),
        ]);
        //加载模板
        $this->assign($result);
        $this->assign('form',$form);
        $this->assign($this->site);
        return $this->fetch();
    }
    
    //定义表单字段
    private function formItems($data=[])
    {
        return [
            'user_id' => [
                'type'        => 'hidden',
                'value'       => $data['user_id'],
            ],
            'user_status' => [
                'type'        => 'select',
                'value'       => $data['user_status'],
                'option'      => ['normal'=>'正常','hidden'=>'隐藏'],
                'required'    => true,
                'placeholder' => '状态',
                'class_row'   => 'float-start mb-3 w-50 pe-3',
                'class_label' => 'visually-hidden',
            ],
            'user_score' => [
                'type'        => 'text',
                'value'       => $data['user_score'],
                'required'    => true,
                'placeholder' => '积 分',
                'class_row'   => 'float-start mb-3 w-50',
                'class_label' => 'visually-hidden',
            ],
            'user_name' => [
                'type'        => 'text',
                'value'       => $data['user_name'],
                'required'    => false,
                'placeholder' => '用户名',
                'class_row'   => 'float-start mb-3 w-50 pe-3',
                'class_label' => 'visually-hidden',
            ],
            'user_pass' => [
                'type'        => 'password',
                'value'       => '',
                'required'    => false,
                'placeholder' => '密码（不修改留空）',
                'class_row'   => 'float-start mb-3 w-50',
                'class_label' => 'visually-hidden',
            ],
            'user_mobile' => [
                'type'        => 'text',
                'value'       => $data['user_mobile'],
                'required'    => true,
                'placeholder' => '手机号',
                'maxlength'   => 11,
                'class_row'   => 'float-start mb-3 w-50 pe-3',
                'class_label' => 'visually-hidden',
            ],
            'user_email' => [
                'type'        => 'text',
                'value'       => $data['user_email'],
                'required'    => false,
                'placeholder' => '邮箱',
                'class_row'   => 'float-start mb-3 w-50',
                'class_label' => 'visually-hidden',
            ],
            'user_roles' => [
                'type'        => 'select',
                'value'       => dcEmpty($data['user_roles'],['user']),
                'option'      => model('common/Option','loglic')->roles(),
                'multiple'    => true,
                'size'        => 3,
                'required'    => true,
                'placeholder' => '角 色',
                'class_row'   => 'float-start mb-3 w-50 pe-3',
                'class_label' => 'visually-hidden',
            ],
            'user_caps' => [
                'type'        => 'textarea',
                'value'       => implode(chr(13),$data['user_caps']),
                'rows'        => 3,
                'required'    => false,
                'placeholder' => '节点',
                'class_row'   => 'float-start mb-3 w-50',
                'class_label' => 'visually-hidden',
            ],
        ];
    }
    
    public function save()
    {
        $post = dcArrayFilter(input('post.'), ['user_status','user_name','user_mobile','user_pass','user_roles','user_caps','user_score']);
        //验证输入
        $modelUser = model('common/User','loglic');
        if(!$modelUser->registerValidate($post)){
            $this->error($modelUser->getError());
        }
        //写入数据
        if($id = model('common/User','loglic')->save($post)){
            $this->success('成功','admin/users/index');
        }else{
            $this->error('失败');
        }
    }
    
    public function update()
    {
        $post = dcArrayFilter(input('post.'), ['user_status','user_name','user_mobile','user_pass','user_roles','user_caps','user_score','user_id']);
        if($id = model('common/User','loglic')->updateId($post['user_id'],$post)){
            $this->success('成功','admin/users/index');
        }else{
            $this->error('失败');
        }
    }
    
    public function delete()
    {
        $result = model('common/User','loglic')->deleteId(input('id/d',0));
        if($result){
            $this->success('成功');
        }else{
            $this->error('失败');
        }
    }
}